#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:

"""
@version: 1.0
@author: readerror
@contact: readerror@readerror.cn
@contact1: readerror000@gmail.com
@site: http://www.readerror.cn
@software: PyCharm
@file: vm.py
@time: 2017/3/22 10:09
"""


class VMApiMixin(object):
    def api_call(self, api, data, async=False, period=60.0):
        NotImplementedError()

    def get_api_result(self, res, api_name=False):
        NotImplementedError()

    def create_vm_instance(self, data, async=False, period=1):
        """
        CreateVmInstance
        :param period:
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'dataDiskOfferingUuids': [],
         'defaultL3NetworkUuid': 'd41725b786c24a5cb67fa0cf6743357b',
         'description': '',
         'imageUuid': 'a353bb510dd74e1f861d0867adb02d90',
         'instanceOfferingUuid': 'ccd0c65e780c4fa6962a792b920b6b3b',
         'l3NetworkUuids': ['d41725b786c24a5cb67fa0cf6743357b'],
         'name': 'test1',
         'session': {'uuid': 'd2363e9a1c2e4f9481f0dec5227dff48'},
         'strategy': 'InstantStart',
         'systemTags': []}
        """
        rsp = self.api_call("org.zstack.header.vm.APICreateVmInstanceMsg", data, async=async, period=period)
        return self.get_api_result(rsp)

    def destroy_vm_instance(self, data, async=False):
        """
        DestroyVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-UNZRYi2T',
                     'uuid': 'f9b4fca934ee4e649e5d3dfbf9349b8e'},
         'uuid': 'fa9b1b8a31864e44a71ca2d67c32744f'}
        """
        rsp = self.api_call('org.zstack.header.vm.APIDestroyVmInstanceMsg', data, async=async)
        return self.get_api_result(rsp)

    def expunge_vm_instance(self, data, async=False):
        """
        ExpungeVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
    {'uuid': 'ccd0c65e780c4fa6962a792b920b6b3b'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIExpungeVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def query_vm_nic(self, data=None, async=False):
        """
        QueryVmNic
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [{'name': 'vmInstanceUuid',
                         'op': '=',
                         'value': 'f3ad3d61ae01472799a5e911ba399956'}],
         'session': {'uuid': '0c0ad05604b44b038268fee754f98c63'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call('org.zstack.header.vm.APIQueryVmNicMsg', data, async=async)
        return self.get_api_result(rsp)

    # 获取物理机上的云主机
    def query_vm_instance(self, data=None, async=False):
        """
        QueryVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [], 'session': {'uuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.vm.APIQueryVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def reboot_vm_instance(self, data, async=False):
        """
        RebootVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-NTErNhUi',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'uuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIRebootVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def request_console_access(self, data, async=False):
        """
        RequestConsoleAccess
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-AM1BdUqG',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'vmInstanceUuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.console.APIRequestConsoleAccessMsg", data, async=async)
        return self.get_api_result(rsp)

    def pause_vm_instance(self, data, async=False):
        """
        PauseVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-LTEumPCp',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'uuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIPauseVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def resume_vm_instance(self, data, async=False):
        """
        ResumeVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-qd7Ur7xX',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'uuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIResumeVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def stop_vm_instance(self, data, async=False):
        """
        StopVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-A4LUqAlW',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'uuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIStopVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    def start_vm_instance(self, data, async=False):
        """
        StartVmInstance
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-f3t0wEka',
                     'uuid': 'd194e096a6fa45948e4761c111e2d1e9'},
         'uuid': '1580992b2b7b4b3cb24af7af3b4000a8'}
        """
        rsp = self.api_call("org.zstack.header.vm.APIStartVmInstanceMsg", data, async=async)
        return self.get_api_result(rsp)

    # 计算规格查询
    def query_instance_offering(self, data=None, async=False):
        """
        QueryInstanceOffering
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [{'name': 'uuid',
                     'op': '=',
                     'value': 'cd07bbddd618413a8e36efd1c541b0b7'}],
        'session': {'uuid': 'f94d60af0ac34ae6a2530a9b9a97cdb8'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.configuration.APIQueryInstanceOfferingMsg", data, async=async)
        return self.get_api_result(rsp)

    # 计算规格创建
    def create_instance_offering(self, data, async=False):
        """
        CreateInstanceOffering
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'allocatorStrategy': 'Mevoco',
         'cpuNum': 1,
         'cpuSpeed': 1,
         'description': '',
         'memorySize': 268435456,
         'name': 'test',
         'session': {'callid': 'api-mNVPIJ2m',
                     'uuid': 'cf46bfd254454b15b2ce75e6ebde5e87'}}
        """
        rsp = self.api_call("org.zstack.header.configuration.APICreateInstanceOfferingMsg", data, async=async)
        return self.get_api_result(rsp)

    # 计算规格销毁
    def delete_instance_offering(self, data, async=False):
        """
        DeleteInstanceOffering
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-T7ib2MIw',
                     'uuid': 'cf46bfd254454b15b2ce75e6ebde5e87'},
         'uuid': '7520186719764d84a4801d593d3ba233'}
        """
        rsp = self.api_call("org.zstack.header.configuration.APIDeleteInstanceOfferingMsg", data, async=async)
        return self.get_api_result(rsp)

    # 计算规格启用
    def enable_instance_offering_state(self, data, async=False):
        """
        ChangeInstanceOfferingState
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-W5w70PJT',
                     'uuid': 'cf46bfd254454b15b2ce75e6ebde5e87'},
         'stateEvent': 'enable',
         'uuid': 'b0c71ee32b064186aab827e00715195d'}
        """
        rsp = self.api_call("org.zstack.header.configuration.APIChangeInstanceOfferingStateMsg", data, async=async)
        return self.get_api_result(rsp)

    # 计算规格停用
    def disable_instance_offering_state(self, data, async=False):
        """
        ChangeInstanceOfferingState
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'callid': 'api-sdc46OeV',
                     'uuid': 'cf46bfd254454b15b2ce75e6ebde5e87'},
         'stateEvent': 'disable',
         'uuid': 'b0c71ee32b064186aab827e00715195d'}
        """
        rsp = self.api_call("org.zstack.header.configuration.APIChangeInstanceOfferingStateMsg", data, async=async)
        return self.get_api_result(rsp)
